<template>
    <div>
          <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    
        <el-card class="box-card">
            <el-button type="primary" @click="showAddGoodsDialog">添加分类</el-button>
            <el-table
                :data="tableData"
                border
                row-key="cat_id"
                style="width: 100%">
                <el-table-column label="#" type="index"></el-table-column>
                <el-table-column prop="cat_name" label="分类名称"></el-table-column>
                <el-table-column prop="cat_deleted" label="是否有效">
                    <template slot-scope="scope">
                        <i style="color: green" v-if="!scope.row.cat_deleted" class="el-icon-success"></i>
                        <i style="color: red;" v-else class="el-icon-error"></i>
                    </template>
                </el-table-column>
                <el-table-column prop="cat_level" label="排序">
                    <template slot-scope="scope">
                        <el-tag size="mini" v-if="scope.row.cat_level === 0" effect="dark">一级</el-tag>
                        <el-tag size="mini" v-if="scope.row.cat_level === 1" type="success" effect="dark">二级</el-tag>
                        <el-tag size="mini" v-if="scope.row.cat_level === 2" type="warning" effect="dark">三级</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="onEditCate(scope.row)" icon="el-icon-edit">编辑
                        </el-button>
                        <el-button type="danger" size="mini" @click=onDeleteCate(scope.row) icon="el-icon-delete">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[1, 2, 5, 10]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>

        </el-card>
        <el-dialog title="添加商品分类" :visible.sync="addGoodsCateVisible">
            <el-form :model="addGoodsCate" :rules="addGoodsCateRules" ref="addGoodsCateForm" label-width="100px">
                <el-form-item label="分类名称" prop="cat_name">
                    <el-input v-model="addGoodsCate.cat_name"></el-input>
                </el-form-item>
                <el-form-item label="父级分类">
                    <el-cascader
                        v-model="selectKeys"
                        @change="onCascaderChange"
                        :options="cascaderOptions"
                        :props="cascaderProps"
                        clearable></el-cascader>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addGoodsCateVisible = false">取 消</el-button>
                <el-button type="primary" @click="onAddGoodsCate">确 定</el-button>
            </div>
        </el-dialog>
        <el-dialog
            title="修改分类"
            :visible.sync="editDialogVisible"
            width="60%">
            <el-form :model="editForm" :rules="editRules" ref="editFormRef" label-width="100px">
                <el-form-item label="分类名称" prop="cat_name">
                    <el-input v-model="editForm.cat_name"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible= false">取 消</el-button>
                <el-button type="primary" @click="onEditBtnClick">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
export default {
    created() {
        this.getCategoriesList()
    },
    name: "Users",
    data() {
        return {
            queryInfo: {
                type: "",
                pagenum: 1,
                pagesize: 10,
            },
            tableData: [],
            addGoodsCateVisible: false,
            addGoodsCate: {
                cat_pid: "0",
                cat_name: "",
                cat_level: "0"
            },
            addGoodsCateRules: {
                cat_name: [
                    {required: true, message: '请输入分类名称', trigger: 'blur'},
                    {min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur'}
                ]
            },
            cascaderOptions: [],
            cascaderProps: {
                checkStrictly: true,
                expandTrigger: 'hover',
                value: "cat_id",
                label: "cat_name",
                children: "children"
            },
            selectKeys: [],
            editDialogVisible: false,
            editForm: {
                cat_name: ""
            },
            editRules: {
                cat_name: [
                    {required: true, message: '请输入分类名称', trigger: 'blur'},
                    {min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur'}
                ]
            },
            curEditId: "",
        }
    },
    methods: {
        async getCategoriesList() {
            let {data: res} = await this.axios.get("categories", {
                params: this.queryInfo
            })
            if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
            this.total = res.data.total;
            this.tableData = res.data.result;
            console.log(res)
        },
        handleSizeChange(val) {
            this.queryInfo.pagesize = val;
            this.getCategoriesList();
        },
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.queryInfo.pagenum = val;
            this.getCategoriesList();
        },
        async showAddGoodsDialog() {
            let {data: res} = await this.axios.get("categories", {
                params: {type: 2}
            })
            if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
            this.cascaderOptions = res.data;
            this.addGoodsCateVisible = true;
        },
        onCascaderChange() {
            console.log("当前被选中的节点id", this.selectKeys);
            if (this.selectKeys.length) {
                this.addGoodsCate.cat_pid = this.selectKeys[this.selectKeys.length - 1];
                this.addGoodsCate.cat_level = this.selectKeys.length;
            } else {
                this.addGoodsCate.cat_pid = "0";
                this.addGoodsCate.cat_level = "0";
            }
        },
        onAddGoodsCate() {
            this.$refs.addGoodsCateForm.validate(async (valid) => {
                if (!valid) return;
                let {data: res} = await this.axios.post("categories", this.addGoodsCate);
                this.addGoodsCate.cat_name = "";
                this.selectKeys = [];
                if (res.meta.status !== 201) return this.$message.error(res.meta.msg);
                this.getCategoriesList();
                this.$message.success(res.meta.msg);
                this.addGoodsCateVisible = false;
            })
        },
        async onDeleteCate(row) {
            let result = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch(err => err);
            console.log(result);
            if (result !== "confirm") return this.$message.info("取消删除")
            let {data: res} = await this.axios.delete(`categories/${row.cat_id}`)
            if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
            this.getCategoriesList()
            this.$message.success(res.meta.msg);
        },
        onEditCate(item) {
            this.curEditId = item.cat_id;
            this.editForm.cat_name = item.cat_name;
            this.editDialogVisible = true;
        },
        onEditBtnClick() {
            this.$refs.editFormRef.validate(async (value) => {
                if (!value) return;
                let {data: res} = await this.axios.put(`categories/${this.curEditId}`, this.editForm);
                if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
                this.getCategoriesList()
                this.$message.success(res.meta.msg);
                this.editDialogVisible = false;
            })
        }
    }
}
</script>

<style scoped>
.el-cascader {
    width: 100%;
}
</style>
